package com.ss.android.newmedia.download;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.design.a;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.newmedia.BaseAppData;

@TargetApi(value = 9)
public class DownloadCompleteReceiver extends BroadcastReceiver {
    public DownloadCompleteReceiver() {

    }

    public void onReceive(Context arg8, Intent arg9) {
        Intent v0_4;
        if (arg9 != null && arg8 != null) {
            String v1 = arg9.getAction();
            if ("android.intent.action.DOWNLOAD_COMPLETE".equals(v1)) {
                Object v0 = arg8.getSystemService("download");
                long v2 = arg9.getLongExtra("extra_download_id", -1);
                if (v2 < 0) {
                    return;
                }

                try {
                    BaseAppData.inst().b(v2);
                } catch (Exception v4) {
                }

                try {
                    DownloadManager.Query v4_1 = new DownloadManager.Query();
                    v4_1.setFilterById(new long[]{v2});
                    Cursor v0_2 = ((DownloadManager) v0).query(v4_1);
                    if (v0_2.moveToNext()) {
                        Uri v2_1 = Uri.parse(v0_2.getString(v0_2.getColumnIndex("local_uri")));
                        String v3 = v0_2.getString(v0_2.getColumnIndex("media_type"));
                        String v4_2 = TTUtils.a(arg8, v2_1);
                        String v0_3 = v0_2.getString(v0_2.getColumnIndex("media_type"));
                        if ((a.isTextEmpty(v0_3)) || !"application/vnd.android.package-archive".equals(v0_3)) {
                            if (a.isTextEmpty(v4_2)) {

                            } else if (v4_2.endsWith(".apk")) {
                                v0_4 = new Intent("android.intent.action.VIEW");
                                v0_4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                v0_4.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                v0_4.setDataAndType(v2_1, v3);
                                arg8.startActivity(v0_4);
                            }
                        }else {
                            label_47:
                            v0_4 = new Intent("android.intent.action.VIEW");
                            v0_4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            v0_4.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            v0_4.setDataAndType(v2_1, v3);
                            arg8.startActivity(v0_4);
                        }
                    }
                    Logger.d("downloadReceiver", "this:" + arg8);
                } catch (Exception v0_1) {
                    Logger.d("downloadReceiver", "open error:" + v0_1);
                }
            }

            if (!"android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED".equals(v1)) {
                return;
            }

            v0_4 = new Intent("android.intent.action.VIEW_DOWNLOADS");
            try {
                v0_4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                arg8.startActivity(v0_4);
            } catch (Exception v0_1) {
            }
        }
    }
}

